<?php 
	namespace Home\Model;
	use Think\Model;

	class ProgramModel extends Model
	{
		protected $trueTableName = "t_program";
		public function get_programs($pro,$page,$rows,$sort,$order,$program_name,$province_name)
		{
			dump($pro);exit();
			$s = [];
			$province_ids = [];
			$page = intval($page);
			$rows = intval($rows);
			$pro && $map['id'] = array("in",$pro);
			// 剧头
			$map['type'] = array("in","1,2");
			// $status && $map['status'] = 0 ;
			$map['program_name'] = array("like","%".$program_name."%");
			$total_res = $this->where($map)->select();
			// dump($total_res);exit;
			$total = count($total_res);
			$res = $this->where($map)->order(array($sort=>$order))->page($page,$rows)->select();
			// dump($res);exit();      //满足查询节目名获得数据
			foreach ($res as $key => $value) {
				$id = $value['id'];
				$res1[] = D("ProgramProvince")->get_provinceid($id,$province_name);
			}
			foreach ($res1 as $key => $value) {
				$res[$key]['province_name'] = $value['province_name'];
			}
			// 省份搜索数据处理
			foreach ($res as $key => $value) {
				if(is_null($value['province_name'])){
					$res_unset[] = $res[$key];
					unset($res[$key]);
				}
			}
			$count = count($res_unset);
			$res = array_values($res);
			// foreach ($res as $key => $value) {
			// 	$res[$key]['code_type'] = $code_type;
			// }
			$s['total'] = $total - $count;
			$s['res'] = $res;
			return $s;
		}

		/**
		 * 修改节目上下线及所属省份
		 * @param  [type] $id       [description]
		 * @param  [type] $province [description]
		 * @param  [type] $status   [description]
		 * @return [type]           [description]
		 */
		public function edit($id,$province,$status)
		{
			$province = explode(",", $province);
			$map["province"] = array("in",$province);
			$res2 = D("Province")->where($map)->select();
			foreach ($res2 as $key => $value) {
				$new_ids[] = $value['id'];    //要修改的省份id数组
			}
			// dump($new_ids);exit();   //新的省份ids		
			$res3 = D("ProgramProvince")->where(["program_id"=>$id])->field('province_id')->select();
			if($res3){
				D("ProgramProvince")->where(array("program_id"=>$id))->delete();
			}	
			foreach ($new_ids as $key => $value) {
				$where["program_id"] = $id;
				$where["province_id"] = $value;
				$res5 = D("ProgramProvince")->add($where);
			}	
			$res1 = $this->where(array("id"=>$id))->fetchSql(true)->save(array("status"=>$status)); 
			($res1 || $res5)?$return = 1:$return = 0;
			return $return;
		}
	}
 ?>